package com.gxa.insurance.service.impl;


import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.ExchangeMapper;
import com.gxa.insurance.pojo.Exchange;
import com.gxa.insurance.service.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author lijunlin
 * @date 2022/6/7
 */
@Service
@Transactional(rollbackFor = {Throwable.class})
public class ExchangeServiceImpl implements ExchangeService {
    @Autowired
    ExchangeMapper exchangeMapper;

    @Override
    public Integer addExchange(Exchange exchange) {

        String random = UUID.randomUUID().toString().replaceAll("-", "");
        exchange.setCode(random);
        exchangeMapper.addExchange(exchange);
        Integer exc_id = exchange.getId();
        return exc_id;
    }

    @Override
    public Map<String, Object>selectByPage(Integer currentPage, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        Integer integer = exchangeMapper.selectAll();
        List<Map<String, Object>> maps = exchangeMapper.selectByPage((currentPage - 1) * pageSize, pageSize);
        map.put("total",integer);
        map.put("list",maps);
        return map;
    }

    @Override
    public Exchange selectById(Integer integer) throws BusinessException {
        Exchange exchange = exchangeMapper.selectById(integer);
        if (exchange == null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return exchange;
    }

    @Override
    public void updateExchange(Exchange exchange) throws Exception {
        if(exchange==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        exchangeMapper.updateExchange(exchange);
    }

    @Override
    public void deleteExchange(Integer integer) {
        exchangeMapper.deleteExchange(integer);
    }

}
